package development;

import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.Key;
import state.Transformer;
import state.component.Event;
import state.component.EventListener;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.util.Color;
import state.util.ImageUtil;
import development.socket.SocketClientUtil;

public class FrmListFreeUser extends GameForm implements EventListener {
  private ImageText text8;
  private int chooseIndex;
  private int pagenummber;
  private int page;
  private Vector listFreeUser;
  private Image onlineImage;
  private Image offlineImage;

  public void init(Hashtable parameters) {
    page = 0;
    chooseIndex = 0;
    listFreeUser = new Vector();
    SocketClientUtil.getUserWantToPlayList(page);
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    onlineImage = ImageUtil.getImage("Tickbox_1.png");
    offlineImage = ImageUtil.getImage("Tickbox_2.png");
  }

  public void draw(Graphics g) {
    g.setColor(0x440000);
    g.fillRect(0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
    g.setColor(0xA53A06);
    g.fillRect(4, 24, GameConstants.SCREEN_WIDTH - 4, GameConstants.SCREEN_HEIGHT - 25);
    text8.drawString(g, "Trở về", Color.WHITE_CODE, GameConstants.SCREEN_WIDTH - 8, GameConstants.SCREEN_HEIGHT - 4,
        GameConstants.BOTTOM_RIGHT_ANCHOR);
    for (int i = 0; i < listFreeUser.size(); i++) {
      String[] user = (String[]) listFreeUser.elementAt(i);
      if (i == chooseIndex) {
        if ("1".equals(user[2])) {
          text8.drawString(g, "Bỏ chọn", Color.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT - 4,
              GameConstants.BOTTOM_HCENTER_ANCHOR);
        } else {
          text8.drawString(g, "Chọn", Color.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT - 4,
              GameConstants.BOTTOM_HCENTER_ANCHOR);
        }
        text8.drawString(g, "Người chơi: " + user[0] + " đang ở " + user[1], Color.BLACK_CODE, 25, 35 + 15 * i,
            GameConstants.VCENTER_LEFT_ANCHOR);
      } else {
        text8.drawString(g, "Người chơi: " + user[0] + " đang ở " + user[1], Color.WHITE_CODE, 25, 35 + 15 * i,
            GameConstants.VCENTER_LEFT_ANCHOR);
      }
      if ("1".equals(user[2])) {
        g.drawImage(onlineImage, 8, 35 + 15 * i, GameConstants.VCENTER_LEFT_ANCHOR);
      } else {
        g.drawImage(offlineImage, 8, 35 + 15 * i, GameConstants.VCENTER_LEFT_ANCHOR);
      }
    }

    text8.drawString(g, "Trang: ", Color.GREEN_CODE, 10, GameConstants.SCREEN_HEIGHT - 20, GameConstants.BOTTOM_LEFT_ANCHOR);
    for (int i = 0; i < pagenummber; i++) {
      if (page == i) {
        text8.drawString(g, (i + 1) + "  ", Color.BLACK_CODE, 45 + i * 10, GameConstants.SCREEN_HEIGHT - 20,
            GameConstants.BOTTOM_LEFT_ANCHOR);
      } else {
        text8.drawString(g, (i + 1) + "  ", Color.GREEN_CODE, 45 + i * 10, GameConstants.SCREEN_HEIGHT - 20,
            GameConstants.BOTTOM_LEFT_ANCHOR);
      }
    }
    if (listFreeUser.size() > 0) {
      text8
          .drawString(g, "Mời vào phòng", Color.WHITE_CODE, 8, GameConstants.SCREEN_HEIGHT - 4, GameConstants.BOTTOM_LEFT_ANCHOR);
    }
    // Draw game service list
  }

  public void setListFreeUser(DataPackage dataPackage) {
    pagenummber = dataPackage.nextInt();
    int listSize = dataPackage.nextInt();
    listFreeUser.removeAllElements();
    for (int i = 0; i < listSize; i++) {
      String[] user = { dataPackage.nextString(), dataPackage.nextString(), "0" };
      listFreeUser.addElement(user);
    }
  }

  public void onActionPerform(Event event) {

  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {

  }

  public void keyReleased(int keyCode) {
    keyCode = Key.getGameKey(keyCode);
    switch (keyCode) {
    case Key.UP:
    case Key.K_2:
      chooseIndex--;
      if (chooseIndex < 0) {
        chooseIndex = listFreeUser.size() - 1;
      }
      break;
    case Key.DOWN:
    case Key.K_8:
      chooseIndex++;
      if (chooseIndex > listFreeUser.size() - 1) {
        chooseIndex = 0;
      }
      break;
    case Key.LEFT:
    case Key.K_4:
      chooseIndex = 0;
      page--;
      if (page <= 0) {
        page = 0;
      }
      SocketClientUtil.getUserWantToPlayList(page);
      break;
    case Key.RIGHT:
    case Key.K_6:
      chooseIndex = 0;
      page++;
      if (page >= pagenummber) {
        page = pagenummber - 1;
      }
      SocketClientUtil.getUserWantToPlayList(page);
      break;
    case Key.FIRE:
    case Key.K_5:
      if (listFreeUser.size() > 0) {
        String[] user = (String[]) listFreeUser.elementAt(chooseIndex);
        if ("0".equals(user[2])) {
          user[2] = "1";
        } else {
          user[2] = "0";
        }
        listFreeUser.setElementAt(user, chooseIndex);
      }
      break;
    case Key.SOFT_RIGHT:
      GameGlobal.nextState(Global.frmTable, null, Transformer.TRANSFORM_WITH_LOADING_FORM);
      Global.frmTable.isReturnFromGame = true;
      break;
    case Key.SOFT_LEFT:
      if (listFreeUser.size() > 0) {
        for (int i = 0; i < listFreeUser.size(); i++) {
          String[] chooseuser = (String[]) listFreeUser.elementAt(i);
          if ("1".equals(chooseuser[2])) {
            SocketClientUtil.inviteUserToTable(chooseuser[0]);
          }
        }
        GameGlobal.nextState(Global.frmTable, null, Transformer.TRANSFORM_WITH_LOADING_FORM);
        Global.frmTable.isReturnFromGame = true;
      }
      break;
    }
  }

  protected void destroy() {
    text8 = null;
    onlineImage = null;
    offlineImage = null;
    listFreeUser = null;
  }
}
